package com.appsindy.common.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.appsindy.common.json.JSONResultCommon;
import com.appsindy.reserv.ReservConstants;
import com.appsindy.reserv.bo.Combo;
import com.appsindy.reserv.service.ReservationServices;
import com.appsindy.reserv.vo.CustomerVO;
import com.appsindy.reserv.vo.GroupInfoVO;
import com.appsindy.reserv.vo.ReserveDetailVO;
import com.appsindy.reserv.vo.ReserveInfoRoomVO;
import com.appsindy.reserv.vo.ReserveInfoVO;
import com.opensymphony.xwork2.Action;



public class ReserveInfoRoomAction extends AppsindyBaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private static final String FWD_BACK = "back";
	
	ReserveInfoRoomVO reserveInfoRoomVO;
	
	private List<Combo> typeOfRoomList;
	private List<Combo> zoneList;
	private List<Combo> floorList;
	private List<Combo> customerList;
	private List<Combo> roomList;
	
	private String[] colServiceDetailId;
	
	private int serviceDetailId;
	private String arrServiceDetailId;	
	private String arrDateFrom;	
	private String arrDateTo;	
	private String arrRoomId;	
	
	private ReservationServices reservationServices;
	
	public String execute() {
		log.info("Init Reservation Information Room ...");

		int serviceHeaderId = 0;
		int groupId			= 0;
		if(reserveInfoRoomVO != null && reserveInfoRoomVO.getServiceHeaderId() > 0){
			serviceHeaderId = reserveInfoRoomVO.getServiceHeaderId();
			if(reserveInfoRoomVO.getGroupInfoVO() != null){
				groupId		= reserveInfoRoomVO.getGroupInfoVO().getGroupId();
			}
		}
		reserveInfoRoomVO = (ReserveInfoRoomVO)getSession().get(ReservConstants.S_RESERVE);
		if(reserveInfoRoomVO == null){
			reserveInfoRoomVO = new ReserveInfoRoomVO();
			reserveInfoRoomVO.setServiceHeaderId(serviceHeaderId);
			GroupInfoVO groupInfoVO = new GroupInfoVO();
			groupInfoVO.setGroupId(groupId);
			reserveInfoRoomVO.setGroupInfoVO(groupInfoVO);
		}
		
		getSession().remove(ReservConstants.S_RESERVE);
		
		return initialReserve();
	}
	
	private String initialReserve(){
		log.info("Initial Reservation Information Room ...");
		setTypeOfRoomList(getReservationServices().getTypeOfRoomCombos());
		setCustomerList(getReservationServices().getCustomerByHeaderCombos(reserveInfoRoomVO.getServiceHeaderId()));
		setRoomList(getReservationServices().getRoomCombos());		
		
		setReserveInfoRoomVO(getReservationServices().selectAvailableRoom(getReserveInfoRoomVO()));
		
		setZoneList(getReservationServices().getZoneCombos(0));
		setFloorList(getReservationServices().getFloorCombos(0, ""));
		
//		if(reserveInfoRoomVO.getTypeOfRoom() > 0){
//			setZoneList(getReservationServices().getZoneCombos(reserveInfoRoomVO.getTypeOfRoom()));
//		}else{
//			setZoneList(new ArrayList<Combo>());
//		}
//		if(reserveInfoRoomVO.getZone() != null && !"".equals(reserveInfoRoomVO.getZone())){
//			setFloorList(getReservationServices().getFloorCombos(reserveInfoRoomVO.getTypeOfRoom(), reserveInfoRoomVO.getZone()));
//		}else{
//			setFloorList(new ArrayList<Combo>());
//		}
		
		return SUCCESS;
	}	
	
	public String search() {
		log.info("Search Reservation Information Room ...");
		setTypeOfRoomList(getReservationServices().getTypeOfRoomCombos());
		setCustomerList(getReservationServices().getCustomerByHeaderCombos(reserveInfoRoomVO.getServiceHeaderId()));
		setRoomList(getReservationServices().getRoomCombos());		
		
		setReserveInfoRoomVO(getReservationServices().selectAvailableRoomWithGo(getReserveInfoRoomVO()));
		
		setZoneList(getReservationServices().getZoneCombos(0));
		setFloorList(getReservationServices().getFloorCombos(0, ""));
		
//		if(reserveInfoRoomVO.getTypeOfRoom() > 0){
//			setZoneList(getReservationServices().getZoneCombos(reserveInfoRoomVO.getTypeOfRoom()));
//		}else{
//			setZoneList(new ArrayList<Combo>());
//		}
//		if(reserveInfoRoomVO.getZone() != null && !"".equals(reserveInfoRoomVO.getZone())){
//			setFloorList(getReservationServices().getFloorCombos(reserveInfoRoomVO.getTypeOfRoom(), reserveInfoRoomVO.getZone()));
//		}else{
//			setFloorList(new ArrayList<Combo>());
//		}
		return SUCCESS;
	}	
	
	public String searchByCustomer() {
		log.info("Search By Customer Reservation Information Room ...");
		
		return initialReserve();
	}		
	
	public String back() {
		log.info("Back ...");
		
		return FWD_BACK;
	}
	
	public String reserve() {
		log.info("Reserve ...");
		
		return processReserve(ReservConstants.ServiceStatusReserved);
	}
	
	public String book() {
		log.info("Book ...");

		return processReserve(ReservConstants.ServiceStatusBooked);
	}

	public String checkin() {
		log.info("Checkin ...");
		
		return processReserve(ReservConstants.ServiceStatusCheckin);
	}	
	
	public String checkout() {
		log.info("Checkout ...");
		
		return processReserve(ReservConstants.ServiceStatusCheckout);
	}	

	private void manageDetailList() throws Exception{
		log.info("manageDetailList ...");
        List<ReserveDetailVO> resultList = new ArrayList<ReserveDetailVO>();
        if(colServiceDetailId != null && colServiceDetailId.length > 0){
            int size			= colServiceDetailId.length;

            ReserveDetailVO vo	= null;
            for(int i=0; i<size; i++){
                vo              = new ReserveDetailVO();
                vo.setServiceDetailId(colServiceDetailId[i]);
                
                resultList.add(vo);
            }
        }
        getReserveInfoRoomVO().setDetailList(resultList);
	}
	
	private String processReserve(String serviceStatus){
		log.info("Process Reserve ...");
		
		try{
			manageDetailList();
		}catch(Exception e){
			
		}
		
		reserveInfoRoomVO = getReservationServices().processReserveRoom(getReserveInfoRoomVO(), serviceStatus);
		
		return initialReserve();
	}
	
	public String deleteDetail(){
		log.info("Delete Detail Reserve ...");
		
		if(serviceDetailId > 0){
			getReservationServices().deleteDetailReserveRoom(serviceDetailId);
		}
		
		return initialReserve();
	}
	
	@SuppressWarnings("finally")
	public String generateCustomerTreeJSON(){
    	log.info("generateCustomerTreeJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Can't check group");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	List<CustomerVO> result = getReservationServices().getCustomerTreeList(reserveInfoRoomVO.getServiceHeaderId());
        	jsonResultCommon = new JSONResultCommon(Action.SUCCESS, result);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
        	getResponse().setContentType("application/json;charset=UTF-8");
        	getResponse().setHeader("Cache-Control", "no-cache");
        	
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    } 	
	
	@SuppressWarnings("finally")
	public String checkCheckinJSON(){
    	log.info("checkCheckinJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", getText("reserve.warn.roomnotavailable4checkin"));
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	List<String> result = getReservationServices().checkRoomBeforeCheckin(arrServiceDetailId, arrDateFrom, arrDateTo, arrRoomId);
        	
        	String errStr = "";
        	String errText = getText("reserve.warn.roomnotavailable4checkin");
        	if(result != null && result.size() > 0){
        		for(String roomNoStr : result){
        			if("".equals(errStr)) 	errStr 	+= roomNoStr + errText;
        			else					errStr 	+= "\n" + roomNoStr + errText;
        		}
        	}
        	ReserveInfoVO infoVO = new ReserveInfoVO();
        	infoVO.setErrMsg(errStr);
        	jsonResultCommon = new JSONResultCommon(Action.SUCCESS, infoVO);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
        	getResponse().setContentType("application/json;charset=UTF-8");
        	getResponse().setHeader("Cache-Control", "no-cache");
        	
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    } 		
	
	@SuppressWarnings("finally")
	public String checkCancelJobJSON(){
    	log.info("checkCancelJobJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", getText("reserve.error.cannotcanceljob"));
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	String errorFlag = getReservationServices().checkServiceCancelJob(reserveInfoRoomVO.getServiceHeaderId(), reserveInfoRoomVO.getServiceStatus());
        	
        	ReserveInfoVO infoVO = new ReserveInfoVO();
        	String errText = getText("reserve.error.cannotcanceljob");
        	if(errorFlag != null && "Y".equals(errorFlag)){
        		infoVO.setErrMsg(errText);
        	}
        	
        	jsonResultCommon = new JSONResultCommon(Action.SUCCESS, infoVO);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
        	getResponse().setContentType("application/json;charset=UTF-8");
        	getResponse().setHeader("Cache-Control", "no-cache");
        	
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    } 		

	public ReserveInfoRoomVO getReserveInfoRoomVO() {
		return reserveInfoRoomVO;
	}

	public void setReserveInfoRoomVO(ReserveInfoRoomVO reserveInfoRoomVO) {
		this.reserveInfoRoomVO = reserveInfoRoomVO;
	}

	public List<Combo> getTypeOfRoomList() {
		return typeOfRoomList;
	}

	public void setTypeOfRoomList(List<Combo> typeOfRoomList) {
		this.typeOfRoomList = typeOfRoomList;
	}

	public ReservationServices getReservationServices() {
		return reservationServices;
	}

	public void setReservationServices(ReservationServices reservationServices) {
		this.reservationServices = reservationServices;
	}

	public List<Combo> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Combo> customerList) {
		this.customerList = customerList;
	}

	public List<Combo> getRoomList() {
		return roomList;
	}

	public void setRoomList(List<Combo> roomList) {
		this.roomList = roomList;
	}

	public String[] getColServiceDetailId() {
		return colServiceDetailId;
	}

	public void setColServiceDetailId(String[] colServiceDetailId) {
		this.colServiceDetailId = colServiceDetailId;
	}

	public int getServiceDetailId() {
		return serviceDetailId;
	}

	public void setServiceDetailId(int serviceDetailId) {
		this.serviceDetailId = serviceDetailId;
	}

	public List<Combo> getZoneList() {
		return zoneList;
	}

	public void setZoneList(List<Combo> zoneList) {
		this.zoneList = zoneList;
	}

	public List<Combo> getFloorList() {
		return floorList;
	}

	public void setFloorList(List<Combo> floorList) {
		this.floorList = floorList;
	}

	public String getArrServiceDetailId() {
		return arrServiceDetailId;
	}

	public void setArrServiceDetailId(String arrServiceDetailId) {
		this.arrServiceDetailId = arrServiceDetailId;
	}

	public String getArrDateFrom() {
		return arrDateFrom;
	}

	public void setArrDateFrom(String arrDateFrom) {
		this.arrDateFrom = arrDateFrom;
	}

	public String getArrDateTo() {
		return arrDateTo;
	}

	public void setArrDateTo(String arrDateTo) {
		this.arrDateTo = arrDateTo;
	}

	public String getArrRoomId() {
		return arrRoomId;
	}

	public void setArrRoomId(String arrRoomId) {
		this.arrRoomId = arrRoomId;
	}
    
}
